package com.pan.hisystem.service.impl;

import com.pan.hisystem.pojo.Menu;
import com.pan.hisystem.mapper.MenuMapper;
import com.pan.hisystem.pojo.User;
import com.pan.hisystem.service.IMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author qingyang
 * @since 2022-03-25
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<Menu> getMenusByUserId() {
        Integer userId = ((User)SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getId();
        System.out.println(userId);
        /*ValueOperations<String,Object> valueOperations=redisTemplate.opsForValue();
        System.out.println(valueOperations);
        //从redis中获取菜单数据
        List<Menu> menus=(List<Menu>) valueOperations.get("menu"+userId);
        for (Menu menu : menus) {
            System.out.println(menu.toString());
        }*/
        //如果为空，则去数据库获取，否则就无需去数据库获取数据，减少与数据库的交互
        /*if(CollectionUtils.isEmpty(menus)){
            System.out.println("进入数据库获取数据");
            menus=menuMapper.getMenusByUserId(userId);
            //将数据设置到redis中
            valueOperations.set("menu"+userId,menus);
        }
        else{
            System.out.println("获取到redis缓存的json数据");
        }*/
        List<Menu> menus=menuMapper.getMenusByUserId(userId);
        return  menus ;
    }

    @Override
    public List<Menu> getMenuWithRole() {
        return menuMapper.getMenuWithRole();
    }
}
